﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace ZhiHeng.OneCard.WpfClient
{
    public partial class MessageControl : Window
    {
        public int ClockNum = 0;
        public string Msg = "";
        public bool MsgType;
     
        /// <summary>
        /// 
        /// </summary>
        /// <param name="clicknum">停留时间,默认2秒</param>
        /// <param name="msg">错误信息</param>
        /// <param name="MsgType">错误信息类型：true：提示信息false：错误信息</param>
        public MessageControl(string msg = "", int clicknum = 2000, bool Msgtype = true)
        {
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            InitializeComponent();
            ClockNum = clicknum;
            Msg = msg;
            MsgType = Msgtype;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (ClockNum > -1)
            {
                Text_Msg.Text = Msg;
                if(MsgType)
                {
                    var fromString = ColorConverter.ConvertFromString((string)"#018817");
                    if (fromString != null)
                        Grid_Back.Background = new SolidColorBrush((Color)fromString);
                }
                else
                {
                    Grid_Back.Background = new SolidColorBrush(Colors.Red);
                }
                DoubleAnimation da = new DoubleAnimation();
                da.BeginTime = TimeSpan.FromMilliseconds(ClockNum / 2);
                da.Duration = TimeSpan.FromMilliseconds(ClockNum);
                da.To = 0;
                this.BeginAnimation(OpacityProperty, da);
                Timer t = new Timer(new TimerCallback(TimeClose));
                t.Change(ClockNum, 0);
            }
        }

        private void TimeClose(object o)
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal, new System.Windows.Forms.MethodInvoker(
             delegate
             {
                 Close();
             }));
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

    }
}
